package es.uji.viselab.world;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Scrollbar;
import java.awt.TextField;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollBar;

import es.uji.viselab.math.Matrix4;
import es.uji.viselab.robot.Robot;

public class TargetSimulatedSelection extends World implements AdjustmentListener {

//	Robot robot;

	JPanel rootPanel;
//	Panel buttonsPanel = new Panel();
//	Panel controlPanelLine1 = new Panel();
//	Panel controlPanelLine2 = new Panel();
	Panel scrollBarsPanel = new Panel();

	Panel scrollBarsPanelJoints = new Panel();
//	Panel[] jointPanel = new Panel[6];
//	Label[] jointLabel = new Label[6];
//	JScrollBar[] jointScrollbar = new JScrollBar[6];
//	TextField[] jointScrollbarTextValue = new TextField[6];
//	TextField[] jointTextValue = new TextField[6];

	Panel scrollBarsPanelXYZ = new Panel();
	Panel[] XYZPanel = new Panel[6];
	Label[] XYZLabel = new Label[6];
	JScrollBar[] XYZScrollbar = new JScrollBar[6];
	TextField[] XYZScrollbarTextValue = new TextField[6];
	TextField[] XYZTextValue = new TextField[6];

//	Label labelIPRobotServer = new Label("IP of robot server: ");
//	TextField textIPRobotServer = new TextField(12);
//	Label labelPortRobotServer = new Label("Port of robot server:");
//	TextField textPortRobotServer = new TextField(5);

	//Label labelJoints = new Label("            Joints                  ");
	Label labelXYZWPR = new Label("Position (XYZ), Target orientation (XYZ)");

	//Button connectRobotButton = new Button("Connect Robot");
	//Button moveBasePositionButton = new Button("Move starting position");
	//Button disconnectRobotButton = new Button("Disconnect Robot");
	//Button isConnectedButton = new Button("Is connected?");
	//Button pingRobotServerButton = new Button("Ping Robot Server");

	

	public TargetSimulatedSelection(Matrix4 m) {
		constructPanel();
		
		targetPose=m;
		XYZScrollbarTextValue[0].setText(String.valueOf((int)(m.m03*1000)));
		XYZTextValue[0].setText(String.valueOf( (int)(m.m03*1000)));
		XYZScrollbar[0].setValue((int)(m.m03*1000));
		XYZScrollbarTextValue[1].setText(String.valueOf((int)(m.m13*1000)));
		XYZTextValue[1].setText(String.valueOf( (int)(m.m13*1000)));
		XYZScrollbar[1].setValue((int)(m.m13*1000));
		XYZScrollbarTextValue[2].setText(String.valueOf((int)(m.m23*1000)));
		XYZTextValue[2].setText(String.valueOf( (int)(m.m23*1000)));
		XYZScrollbar[2].setValue((int)(m.m23*1000));
		initialize();
	}

	private void initialize() {
		for (int i = 0; i < 6; i++) {
			XYZScrollbar[i].addAdjustmentListener(this);
		}

	}

	protected void constructPanel() {

		rootPanel = new JPanel();
		rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.PAGE_AXIS));

		scrollBarsPanel.add(scrollBarsPanelXYZ);

		scrollBarsPanelXYZ.setLayout(new GridLayout(7, 1));
		scrollBarsPanelXYZ.add(labelXYZWPR);

		for (int i = 0; i < 6; i++) {
			XYZPanel[i] = new Panel();
			XYZPanel[i].setLayout(new FlowLayout());
			XYZLabel[i] = new Label();
			if (i == 0 || i == 3)
				XYZLabel[i].setText("x");
			else if (i == 1 || i == 4)
				XYZLabel[i].setText("y");
			else if (i == 2 || i == 5)
				XYZLabel[i].setText("z");
			XYZScrollbar[i] = new JScrollBar(Scrollbar.HORIZONTAL, 10, 20, -1000, 2000);
			XYZScrollbar[i].setUnitIncrement(10);
			XYZScrollbarTextValue[i] = new TextField(4);
			XYZTextValue[i] = new TextField(4);
			XYZScrollbarTextValue[i].setText("0");
			XYZTextValue[i].setText(String.valueOf(0));

			XYZPanel[i].add(XYZLabel[i]);
			XYZPanel[i].add(XYZScrollbar[i]);
			XYZPanel[i].add(XYZScrollbarTextValue[i]);
			XYZPanel[i].add(XYZTextValue[i]);
			scrollBarsPanelXYZ.add(XYZPanel[i]);
		}

		rootPanel.add(scrollBarsPanel);

	}

	public JPanel getPanel() {

		return rootPanel;
	}

	@Override
	public void adjustmentValueChanged(AdjustmentEvent e) {
		if (e.getSource().equals(XYZScrollbar[0]) || e.getSource().equals(XYZScrollbar[1])
				|| e.getSource().equals(XYZScrollbar[2]) || e.getSource().equals(XYZScrollbar[3])
				|| e.getSource().equals(XYZScrollbar[4]) || e.getSource().equals(XYZScrollbar[5])) {

			for (int i = 0; i < 6; i++) {
				XYZScrollbarTextValue[i].setText(String.valueOf(XYZScrollbar[i].getValue()));
			}
			double[] v = { XYZScrollbar[0].getValue(), XYZScrollbar[1].getValue(),
					XYZScrollbar[2].getValue(), XYZScrollbar[3].getValue(),
					XYZScrollbar[4].getValue(), XYZScrollbar[5].getValue() };
			for (int i = 0; i < 6; i++)
				v[i] = v[i] / 1000;
			targetPose = Matrix4.getTranslation(v[0], v[1], v[2]);
			notifyWorldChanged(this);
		}

	}

	@Override
	public String getPanelTitle() {
		return "Target pose selection";
	}

	@Override
	public int panelWidth() {
		return 200;
	}

	@Override
	public int panelHeight() {
		return 400;
	}

}